﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BugzX.Repositories
{
    public static class ProjectRepository
    {
        public static bool IsNameUnique(string name)
        {
            var context = new BugzXDBEntities();

            var b = from p in context.Projects
                    where p.Name.ToLower().Trim() == name.ToLower().Trim()
                    select p;

            return b.Count() == 0;
        }

        public static Project GetByName(string name)
        {
            var context = new BugzXDBEntities();

            var b = from p in context.Projects
                    where p.Name.ToLower().Trim() == name.ToLower().Trim()
                    select p;

            return b.FirstOrDefault();
        }

        public static List<ProjectComponent> GetComponentsByProjectName(string name)
        {
            var context = new BugzXDBEntities();

            var pcs = from pc in context.ProjectComponents
                      join p in context.Projects
                         on pc.ProjectId equals p.ProjectId
                      where p.Name.ToLower().Trim() == name.ToLower().Trim()
                      select pc;

            return pcs.ToList();
        }
    }
}
